<ResourceDictionary
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d"
  xmlns:local="clr-namespace:TomsToolbox.Wpf"
  xmlns:controls="clr-namespace:TomsToolbox.Wpf.Controls"
  xmlns:composition="clr-namespace:TomsToolbox.Wpf.Composition"
  xmlns:converters="clr-namespace:TomsToolbox.Wpf.Converters">

  <Style TargetType="TextBlock" x:Key="{x:Static local:ResourceKeys.AutoToolTipTextBoxStyle}">
    <Style.Triggers>
      <MultiTrigger>
        <MultiTrigger.Conditions>
          <Condition Property="local:TextBlockHelper.IsAutomaticToolTipEnabled" Value="True" />
          <Condition Property="local:TextBlockHelper.IsTextTrimmed" Value="True" />
        </MultiTrigger.Conditions>
        <Setter Property="ToolTip" Value="{Binding Path=Text, RelativeSource={x:Static RelativeSource.Self}}" />
      </MultiTrigger>
    </Style.Triggers>
  </Style>

  <ControlTemplate TargetType="ContentControl" x:Key="SharedWidthContainerTemplate">
    <Grid>
      <Grid.ColumnDefinitions>
        <ColumnDefinition SharedSizeGroup="SharedWidthDefaultGroup" />
      </Grid.ColumnDefinitions>
      <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                        VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                        ContentStringFormat="{TemplateBinding ContentStringFormat}"
                        ContentTemplate="{TemplateBinding ContentTemplate}"
                        ContentTemplateSelector="{TemplateBinding ContentTemplateSelector}" />
    </Grid>
  </ControlTemplate>

  <Style TargetType="ContentControl" x:Key="{x:Static local:ResourceKeys.SharedWidthContainerStyle}">
    <Setter Property="Template" Value="{StaticResource SharedWidthContainerTemplate}" />
    <Setter Property="Focusable" Value="False" />
  </Style>

  <Style TargetType="{x:Type controls:SharedWidthHeaderedContentControl}">
    <Setter Property="VerticalContentAlignment" Value="Center" />
    <Setter Property="Focusable" Value="False" />
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type controls:SharedWidthHeaderedContentControl}">
          <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
            <DockPanel Margin="{TemplateBinding Padding}">
              <ContentControl DockPanel.Dock="Left" Style="{DynamicResource {x:Static local:ResourceKeys.SharedWidthContainerStyle}}"
                              Margin="{TemplateBinding HeaderPadding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                              Content="{TemplateBinding Header}" ContentStringFormat="{TemplateBinding HeaderStringFormat}"
                              ContentTemplate="{TemplateBinding HeaderTemplate}" ContentTemplateSelector="{TemplateBinding HeaderTemplateSelector}"/>
              <Decorator DockPanel.Dock="Left" Width="10" />
              <ContentPresenter VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}"
                                ContentTemplate="{TemplateBinding ContentTemplate}" ContentTemplateSelector="{TemplateBinding ContentTemplateSelector}" />
            </DockPanel>
          </Border>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

  <!--List box/list view with check boxes: Style to be applied to the check box inside item or cell template.
      See e.g. http://msdn.microsoft.com/en-us/library/ms754143.aspx />
      -->
  <Style TargetType="CheckBox" x:Key="{x:Static local:ResourceKeys.ListBoxItemCheckBoxStyle}">
    <Setter Property="Focusable" Value="False" />
    <Setter Property="DockPanel.Dock" Value="Left" />
    <Setter Property="Margin" Value="3" />
    <Setter Property="IsChecked" Value="{Binding Path=IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBoxItem}, Mode=TwoWay}" />
    <Style.Triggers>
      <DataTrigger Binding="{Binding Path=SelectionMode, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBox}}" Value="Single">
        <Setter Property="Visibility" Value="Collapsed" />
      </DataTrigger>
    </Style.Triggers>
  </Style>

  <!-- Data grid with check boxes for row selection: Style to be applied to the check box inside the row header template. -->
  <Style TargetType="CheckBox" x:Key="{x:Static local:ResourceKeys.DataGridRowCheckBoxStyle}">
    <Setter Property="Focusable" Value="False" />
    <Setter Property="DockPanel.Dock" Value="Left" />
    <Setter Property="Margin" Value="2" />
    <Setter Property="IsChecked"
            Value="{Binding Path=IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGridRow}, Mode=TwoWay}" />
    <Style.Triggers>
      <DataTrigger Binding="{Binding Path=SelectionMode, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}}" Value="Single">
        <Setter Property="Visibility" Value="Hidden" />
      </DataTrigger>
    </Style.Triggers>
  </Style>

  <!-- A copy of the original data grid cell style, extended with
    VerticalAlignment binding to control the vertical alignment of the content via the DataGrid.VerticalContentAlignment property.
    Also added support for padding.
  -->
  <Style TargetType="{x:Type DataGridCell}" x:Key="{x:Static local:ResourceKeys.DataGridCellStyle}">
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="BorderBrush" Value="Transparent" />
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type DataGridCell}">
          <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
            <ContentPresenter Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{Binding VerticalContentAlignment, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}}" />
          </Border>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
    <Style.Triggers>
      <Trigger Property="IsSelected" Value="True">
        <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" />
        <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
      </Trigger>
      <Trigger Property="IsKeyboardFocusWithin" Value="True">
        <Setter Property="BorderBrush" Value="{DynamicResource {x:Static DataGrid.FocusBorderBrushKey}}" />
      </Trigger>
    </Style.Triggers>
  </Style>

  <!-- A control that shows validation errors in the tool tip. -->
  <Style x:Key="{x:Static local:ResourceKeys.ControlWithValidationErrorToolTipStyle}" TargetType="{x:Type Control}">
    <Style.Triggers>
      <Trigger Property="Validation.HasError" Value="True">
        <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}" />
      </Trigger>
    </Style.Triggers>
  </Style>

  <ControlTemplate x:Key="LoadingIndicatorTemplate" TargetType="{x:Type controls:LoadingIndicator}">
    <Grid Cursor="Wait">
      <Grid.Style>
        <Style TargetType="Grid">
          <Setter Property="Opacity" Value="0" />
        </Style>
      </Grid.Style>
      <Grid.Background>
        <SolidColorBrush x:Name="backgroundBrush" />
      </Grid.Background>
      <Grid.Resources>
        <Storyboard x:Key="OpacityStoryboard">
          <DoubleAnimation Storyboard.TargetProperty="Opacity" BeginTime="0:0:0.5" Duration="0:0:1" To="1" FillBehavior="HoldEnd" />
          <ColorAnimation Storyboard.TargetName="backgroundBrush" Storyboard.TargetProperty="Color" Duration="0:0:1" To="{TemplateBinding ActiveBackgroundColor}" FillBehavior="HoldEnd" />
        </Storyboard>
      </Grid.Resources>
      <Grid.Triggers>
        <EventTrigger RoutedEvent="FrameworkElement.Loaded">
          <BeginStoryboard Storyboard="{StaticResource OpacityStoryboard}" />
        </EventTrigger>
      </Grid.Triggers>
      <Decorator LayoutTransform="{TemplateBinding AnimationLayoutTransform}">
        <Canvas Width="100" Height="100" RenderTransformOrigin="0.5,0.5" VerticalAlignment="Center" HorizontalAlignment="Center" Opacity="0.5">
          <Canvas.Resources>
            <Storyboard x:Key="RotationStoryboard">
              <DoubleAnimation Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)" To="360" Duration="0:0:5" RepeatBehavior="Forever" />
            </Storyboard>
            <Style TargetType="Ellipse">
              <Setter Property="Height" Value="20" />
              <Setter Property="Width" Value="20" />
              <Setter Property="Canvas.Left" Value="80" />
              <Setter Property="Canvas.Top" Value="40" />
              <Setter Property="RenderTransformOrigin" Value="-1.5,0.5" />
            </Style>
          </Canvas.Resources>
          <Canvas.RenderTransform>
            <RotateTransform />
          </Canvas.RenderTransform>
          <Canvas.Triggers>
            <EventTrigger RoutedEvent="FrameworkElement.Loaded">
              <BeginStoryboard Storyboard="{StaticResource RotationStoryboard}" />
            </EventTrigger>
          </Canvas.Triggers>
          <Ellipse Fill="#11000000">
            <Ellipse.RenderTransform>
              <RotateTransform Angle="0" />
            </Ellipse.RenderTransform>
          </Ellipse>
          <Ellipse Fill="#33000000">
            <Ellipse.RenderTransform>
              <RotateTransform Angle="45" />
            </Ellipse.RenderTransform>
          </Ellipse>
          <Ellipse Fill="#55000000">
            <Ellipse.RenderTransform>
              <RotateTransform Angle="90" />
            </Ellipse.RenderTransform>
          </Ellipse>
          <Ellipse Fill="#77000000">
            <Ellipse.RenderTransform>
              <RotateTransform Angle="135" />
            </Ellipse.RenderTransform>
          </Ellipse>
          <Ellipse Fill="#99000000">
            <Ellipse.RenderTransform>
              <RotateTransform Angle="180" />
            </Ellipse.RenderTransform>
          </Ellipse>
          <Ellipse Fill="#BB000000">
            <Ellipse.RenderTransform>
              <RotateTransform Angle="225" />
            </Ellipse.RenderTransform>
          </Ellipse>
          <Ellipse Fill="#DD000000">
            <Ellipse.RenderTransform>
              <RotateTransform Angle="270" />
            </Ellipse.RenderTransform>
          </Ellipse>
          <Ellipse Fill="#FF000000">
            <Ellipse.RenderTransform>
              <RotateTransform Angle="315" />
            </Ellipse.RenderTransform>
          </Ellipse>
        </Canvas>
      </Decorator>
      <ContentPresenter Content="{TemplateBinding Header}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" />
    </Grid>
  </ControlTemplate>

  <Style TargetType="{x:Type controls:LoadingIndicator}">
    <Setter Property="HorizontalAlignment" Value="Stretch" />
    <Setter Property="VerticalAlignment" Value="Stretch" />
    <Setter Property="HorizontalContentAlignment" Value="Center" />
    <Setter Property="VerticalContentAlignment" Value="Center" />
    <Style.Triggers>
      <Trigger Property="IsActive" Value="True">
        <Setter Property="Template" Value="{StaticResource LoadingIndicatorTemplate}" />
      </Trigger>
    </Style.Triggers>
  </Style>

  <Style TargetType="{x:Type controls:InPlaceEdit}">
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type controls:InPlaceEdit}">
          <Border Background="{TemplateBinding Background}"
                  BorderBrush="{TemplateBinding BorderBrush}"
                  BorderThickness="{TemplateBinding BorderThickness}">
            <Grid>
              <TextBlock Text="{TemplateBinding Text}" HorizontalAlignment="Left" Margin="2,0" TextTrimming="{TemplateBinding TextTrimming}" />
              <TextBox x:Name="PART_TextBox" Visibility="Hidden" BorderThickness="0" Padding="0" />
            </Grid>
          </Border>
          <ControlTemplate.Triggers>
            <Trigger Property="HasErrors" Value="True">
              <Setter Property="Foreground" Value="Red" />
            </Trigger>
          </ControlTemplate.Triggers>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

  <Style TargetType="{x:Type controls:Map}">
    <Setter Property="UseLayoutRounding" Value="False" />
    <Setter Property="SnapsToDevicePixels" Value="False" />
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type controls:Map}">
          <Grid x:Name="PART_Viewport" Background="LightGray">
            <Viewbox Stretch="UniformToFill"
                     HorizontalAlignment="Center"
                     VerticalAlignment="Center"
                     Width="{Binding ViewportSize.X, RelativeSource={RelativeSource TemplatedParent}}"
                     Height="{Binding ViewportSize.Y, RelativeSource={RelativeSource TemplatedParent}}">
              <Viewbox.Resources>
                <TranslateTransform x:Key="OffsetTransform"
                                    X="{Binding Offset.X, RelativeSource={RelativeSource TemplatedParent}}"
                                    Y="{Binding Offset.Y, RelativeSource={RelativeSource TemplatedParent}}" />
                <ScaleTransform x:Key="ScaleTransform"
                                ScaleX="{Binding ZoomFactor, RelativeSource={RelativeSource TemplatedParent}}"
                                ScaleY="{Binding ZoomFactor, RelativeSource={RelativeSource TemplatedParent}}" />
                <TranslateTransform x:Key="LeftShadowOffsetTransform" X="-1" />
                <TranslateTransform x:Key="RightShadowOffsetTransform" X="1" />
              </Viewbox.Resources>
              <Grid Width="1" Height="1" RenderTransform="{StaticResource OffsetTransform}">
                <Grid RenderTransform="{StaticResource ScaleTransform}"
                      RenderTransformOrigin="{Binding ZoomingPoint, RelativeSource={RelativeSource TemplatedParent}}">
                  <controls:PixelSnapper Viewport="{Binding ElementName=PART_Viewport}">
                    <Grid>
                      <controls:MapTile x:Name="PART_World"
                                             Viewport="{Binding ElementName=PART_Viewport}"
                                             ImageProvider="{TemplateBinding ImageProvider}" />
                      <controls:MapTile Viewport="{Binding ElementName=PART_Viewport}"
                                             ImageProvider="{TemplateBinding ImageProvider}"
                                             RenderTransform="{StaticResource LeftShadowOffsetTransform}" />
                      <controls:MapTile Viewport="{Binding ElementName=PART_Viewport}"
                                             ImageProvider="{TemplateBinding ImageProvider}"
                                             RenderTransform="{StaticResource RightShadowOffsetTransform}" />
                    </Grid>
                  </controls:PixelSnapper>
                </Grid>
              </Grid>
            </Viewbox>
            <ContentPresenter UseLayoutRounding="True" />
          </Grid>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

  <ControlTemplate x:Key="MenuGroupItemTemplate" TargetType="{x:Type GroupItem}">
    <StackPanel>
      <Separator Style="{DynamicResource {x:Static MenuItem.SeparatorStyleKey}}"/>
      <ItemsPresenter />
    </StackPanel>
  </ControlTemplate>

  <ControlTemplate x:Key="FirstMenuGroupItemTemplate" TargetType="{x:Type GroupItem}">
    <ItemsPresenter />
  </ControlTemplate>

  <GroupStyle x:Key="MenuGroupStyle">
    <GroupStyle.ContainerStyle>
      <Style TargetType="GroupItem">
        <Setter Property="Template" Value="{StaticResource MenuGroupItemTemplate}"/>
        <Style.Triggers>
          <DataTrigger Binding="{Binding RelativeSource={RelativeSource PreviousData}}" Value="{x:Null}">
            <Setter Property="Template" Value="{StaticResource FirstMenuGroupItemTemplate}"/>
          </DataTrigger>
        </Style.Triggers>
      </Style>
    </GroupStyle.ContainerStyle>
  </GroupStyle>

  <Style x:Key="CompositeMenuItemStyle" TargetType="{x:Type MenuItem}">
    <Setter Property="Header" Value="{Binding Header}"/>
    <Setter Property="Icon" Value="{Binding Icon, Converter={x:Static converters:ImageSourceToImageConverter.Default}}"/>
    <Setter Property="ToolTip" Value="{Binding Description}"/>
    <Setter Property="IsCheckable" Value="{Binding IsCheckable}"/>
    <Setter Property="local:StyleBindings.GroupStyle" Value="{StaticResource MenuGroupStyle}"/>
    <Setter Property="local:StyleBindings.GroupDescriptions">
      <Setter.Value>
        <local:GroupDescriptionCollection>
          <PropertyGroupDescription PropertyName="GroupName"/>
        </local:GroupDescriptionCollection>
      </Setter.Value>
    </Setter>
    <Setter Property="local:StyleBindings.Behaviors">
      <Setter.Value>
        <local:BehaviorCollection>
          <composition:ItemsControlCompositionBehavior RegionIdBinding="{Binding SubRegionId}"/>
        </local:BehaviorCollection>
      </Setter.Value>
    </Setter>
    <Style.Triggers>
      <DataTrigger Binding="{Binding IsCheckable}" Value="True">
        <Setter Property="IsChecked" Value="{Binding IsChecked}"/>
      </DataTrigger>
      <Trigger Property="Role" Value="SubmenuItem">
        <Setter Property="Command" Value="{Binding Command}"/>
      </Trigger>
      <Trigger Property="Role" Value="TopLevelItem">
        <Setter Property="Command" Value="{Binding Command}"/>
      </Trigger>
    </Style.Triggers>
  </Style>

  <Style TargetType="{x:Type controls:CompositeContextMenu}" BasedOn="{StaticResource {x:Type ContextMenu}}">
    <Setter Property="local:StyleBindings.GroupStyle" Value="{StaticResource MenuGroupStyle}"/>
    <Setter Property="local:StyleBindings.GroupDescriptions">
      <Setter.Value>
        <local:GroupDescriptionCollection>
          <PropertyGroupDescription PropertyName="GroupName"/>
        </local:GroupDescriptionCollection>
      </Setter.Value>
    </Setter>
    <Setter Property="ItemContainerStyle" Value="{StaticResource CompositeMenuItemStyle}"/>
  </Style>

  <Style x:Key="{x:Static local:ResourceKeys.CompositeMenuStyle}" TargetType="{x:Type Menu}">
    <Setter Property="ItemContainerStyle" Value="{StaticResource CompositeMenuItemStyle}"/>
  </Style>

</ResourceDictionary>
